package com.aistar.serviceVO.impl;

import com.aistar.mapper.ProgramCollectMapper;
import com.aistar.mapper.ProgramMapper;
import com.aistar.mapper.CustomerMapper;
import com.aistar.pojo.*;
import com.aistar.pojoVO.ProgramAndCollectVO;
import com.aistar.serviceVO.ProgramAndCollectVOService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProgramAndCollectVOServiceImpl implements ProgramAndCollectVOService {
    private final ProgramCollectMapper programCollectMapper;
    private final ProgramMapper programMapper;
    private final CustomerMapper customerMapper;

    @Autowired
    public ProgramAndCollectVOServiceImpl(ProgramCollectMapper programCollectMapper,
                                         ProgramMapper programMapper,
                                         CustomerMapper customerMapper) {
        this.programCollectMapper = programCollectMapper;
        this.programMapper = programMapper;
        this.customerMapper = customerMapper;
    }

    @Override
    public ServerResponse getByCustomerId(Integer customerId) {
        ProgramCollectExample example = new ProgramCollectExample();
        example.createCriteria()
                .andCustomerIdEqualTo(customerId)
                .andStatusEqualTo(MessageUtil.STATUS_SUCCESS);
        example.setOrderByClause("program_collect_time desc");
        List<ProgramCollect> programCollectList = programCollectMapper.selectByExample(example);
        System.out.println(programCollectList);
        if (programCollectList != null && programCollectList.size() > 0) {
            List<ProgramAndCollectVO> programAndCollectVOList = new ArrayList<>();
            for (ProgramCollect programCollect : programCollectList) {
                Program program = programMapper.selectByPrimaryKey(programCollect.getProgramId());
                Customer customer = customerMapper.selectByPrimaryKey(programCollect.getCustomerId());
                programAndCollectVOList.add(new ProgramAndCollectVO(programCollect, program, customer));
            }
            return ServerResponse.getSuccess(programAndCollectVOList);
        }
        return ServerResponse.getFailed();
    }
}
